package com.tanxi521.springboot.controller;

import com.tanxi521.springboot.bean.OnlinePlayerBean;
import com.tanxi521.springboot.pojo.APIResult;
import com.tanxi521.springboot.pojo.OnlinePlayer;
import com.tanxi521.springboot.service.OnlinePlayerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author: i-xiaotian
 * @Date: 2022/4/10 1:37
 * @EMAIL: 1254806967@qq.com
 */

@RestController
@RequestMapping("/index")
@CrossOrigin
public class IndexController {

    @Autowired
    OnlinePlayerService onlinePlayerService;

    @GetMapping("/online")
    public APIResult getOnlinePlayersNum(Long end) {
        List<OnlinePlayer> onlinePlayerList = onlinePlayerService.queryPlayerOnlineNumList(end);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        for (OnlinePlayer onlinePlayer : onlinePlayerList) {
            onlinePlayer.setTime(sdf.format(new Date(Long.parseLong(onlinePlayer.getTime()))));
        }
        OnlinePlayerBean onlinePlayerBean = new OnlinePlayerBean();
        onlinePlayerBean.setOnlinePlayerNumList(onlinePlayerList);
        return new APIResult(onlinePlayerBean, APIResult.SUCCESS, "获取在线人数列表成功");
    }

}




